package cn.ncupyh;

import java.util.Random;
import java.util.Scanner;
/*键盘输入字符,打乱顺序并输出*/
public class interuptChar {
    public static void main(String[] args) {
        Scanner sin = new Scanner(System.in);
        System.out.println("请输入字符串:");
        String s = sin.next();
        //打乱并输出
        String result = interupt(s);
        System.out.println(result);
    }
    //将字符串存入字符数组当中,并打乱
    public static String interupt(String s){
        //将字符串存到字符数组
        char[] arr = s.toCharArray();
        //建立一个随机数
        Random r = new Random();
        //索引交换
        for (int j = 0; j < (s.length() / 2); j++) {
            int m = r.nextInt(s.length()-1);
            int i = r.nextInt(s.length()-1);
            char tmp = arr[i];
            arr[i] = arr[m];
            arr[m] = tmp;
        }
        String result = new String(arr);
        return result;
    }
}
